from django.db import models
from django.contrib.auth.models import AbstractUser, Group
from xadmin.utils import  get_img_path, get_file_path, get_video_path
from django.shortcuts import reverse
from django.utils import timezone


class UserProfile(AbstractUser):
    '''
    用户表
    '''
    chinese_name = models.CharField(max_length=50, verbose_name='姓名')
    mobile = models.CharField(max_length=11, verbose_name='手机')
    site = models.ForeignKey('Sites', null=True, blank=True, on_delete=models.CASCADE, verbose_name='所属站点')
    user_group = models.ManyToManyField('UserGroup', null=True, blank=True, related_name="user_set", verbose_name='角色')

    class Meta:
        verbose_name = '用户管理'
        verbose_name_plural = verbose_name

    def __str__(self):
        return self.chinese_name


class GroupPermission(models.Model):
    group = models.ForeignKey('UserGroup', on_delete=models.CASCADE, verbose_name='角色')
    permission_type = models.CharField(max_length=50, verbose_name='权限类型')
    permission_slug = models.CharField(max_length=50, verbose_name='权限标识')
    site = models.ForeignKey('Sites', on_delete=models.CASCADE, verbose_name='所属站点')

    class Meta:
        verbose_name = '角色权限'
        verbose_name_plural = verbose_name
        unique_together = ('group', 'permission_type', 'permission_slug')

    def __str__(self):
        return self.group.name


class UserGroup(models.Model):
    site = models.ForeignKey('Sites', null=True, blank=True, on_delete=models.CASCADE, verbose_name='所属站点')
    name = models.CharField(max_length=50, verbose_name='名称')


class Sites(models.Model):
    '''站点表'''
    name = models.CharField(max_length=100, verbose_name='站点名称')
    logo = models.ImageField(default='', upload_to=get_img_path, blank=True, verbose_name='站点logo')
    mobile_logo = models.ImageField(default='', upload_to=get_img_path, blank=True, verbose_name='手机logo')
    status = models.BooleanField(default=True, verbose_name='站点状态')
    domains = models.CharField(default='', blank=True, max_length=500, verbose_name='域名', help_text='多个域名用逗号隔开')
    title = models.CharField(null=True, blank=True, max_length=200, verbose_name='网站标题')
    keyword = models.CharField(null=True, blank=True, max_length=200, verbose_name='网站关键字')
    des = models.TextField(null=True, blank=True, verbose_name='网站描述')
    copyright = models.CharField(max_length=200, null=True, blank=True, verbose_name='版权信息')
    beian = models.CharField(max_length=50, null=True, blank=True, verbose_name='ICP备案号')
    address = models.CharField(max_length=200, null=True, blank=True, verbose_name='地址')
    zip_code = models.CharField(max_length=10, null=True, blank=True, verbose_name='邮编')

    class Meta:
        verbose_name = '站点管理'
        verbose_name_plural = verbose_name

    def __str__(self):
        return self.name


class Templates(models.Model):
    '''模板表'''
    site = models.ForeignKey(Sites, on_delete=models.CASCADE, verbose_name='站点ID')
    name = models.CharField(max_length=200, verbose_name='模板名称')
    path = models.CharField(max_length=255, verbose_name='模板路径', help_text='templates目录为根,如www/detail.html')
    picture = models.ImageField(upload_to=get_img_path, null=True, blank=True, help_text='缩略图')
    router = models.CharField(max_length=100, null=True, blank=True, verbose_name='路由')

    class Meta:
        verbose_name = '模板管理'
        verbose_name_plural = verbose_name

    def __str__(self):
        return self.name


class NodeGroup(models.Model):
    name = models.CharField(max_length=200, verbose_name='栏目组名称')
    index_name = models.CharField(max_length=100, null=True, blank=True, unique=True, verbose_name='组索引')
    picture = models.ImageField(upload_to=get_img_path, null=True, blank=True, verbose_name='栏目组图片')
    site = models.ForeignKey(Sites, on_delete=models.CASCADE, verbose_name='所属站点')
    brief = models.TextField(null=True, blank=True, verbose_name='简介')
    content = models.TextField(null=True, blank=True, verbose_name='详情')
    node = models.ManyToManyField('Node', related_name='node_group', null=True, blank=True, verbose_name='组内栏目')


    class Meta:
        verbose_name = '栏目组管理'
        verbose_name_plural = verbose_name

    def __str__(self):
        return self.name


node_type_choices = (
    ('', '-----'),
    ('content', '内容'),
    ('product', '产品'),
    ('job', '招聘')
)

template_type_choices = (
    ('gather', '聚合页'),
    ('ilist', '列表页'),
)

open_type_choices = (
    ('_self', '当前窗口'),
    ('_blank', '新窗口'),
)

class Node(models.Model):
    parent_node = models.ForeignKey('self', null=True, blank=True, on_delete=models.CASCADE, verbose_name='父栏目')
    name = models.CharField(max_length=200, verbose_name='栏目名称')
    sub_name = models.CharField(max_length=200, null=True, blank=True, verbose_name='副标题')
    slogen1 = models.CharField(max_length=200, null=True, blank=True, verbose_name='标语1')
    slogen2 = models.CharField(max_length=200, null=True, blank=True, verbose_name='标语2')
    order_num = models.IntegerField(default=0, verbose_name='排序')
    index_name = models.CharField(max_length=200, null=True, blank=True, unique=True, verbose_name='栏目索引')
    force_url = models.CharField(max_length=255, null=True, blank=True, verbose_name='强链')
    node_type = models.CharField(max_length=20, null=True, blank=True, choices=node_type_choices, default='', verbose_name='内容类型')
    template_type = models.CharField(max_length=20, choices=template_type_choices, default='gather', verbose_name='模版类型')
    open_type = models.CharField(max_length=20, choices=open_type_choices, default='_self', verbose_name='打开方式')
    site = models.ForeignKey(Sites, on_delete=models.CASCADE, verbose_name='站点')
    brief = models.TextField(null=True, blank=True, verbose_name='简介')
    content = models.TextField(null=True, blank=True, verbose_name='内容')
    is_active = models.BooleanField(default=True, verbose_name='是否激活')
    is_search = models.BooleanField(default=True, verbose_name='是否能被搜索')
    gather_template = models.ForeignKey(Templates, related_name='gather_node_set', null=True, blank=True, on_delete=models.SET_NULL, verbose_name='聚合页模板')
    list_template = models.ForeignKey(Templates, related_name='list_node_set', null=True, blank=True, on_delete=models.SET_NULL, verbose_name='列表页模板')
    content_template = models.ForeignKey(Templates, related_name='content_node_set', null=True, blank=True, on_delete=models.SET_NULL, verbose_name='内容页模板')
    router = models.CharField(max_length=100, null=True, blank=True, verbose_name='路由')
    icon = models.ImageField(upload_to=get_img_path, null=True, blank=True,
                                verbose_name='图标')
    picture = models.ImageField(upload_to=get_img_path, null=True, blank=True,
                                verbose_name='banner图片')
    cover_picture = models.ImageField(upload_to=get_img_path, null=True, blank=True,
                                verbose_name='封面图片')
    background_picture = models.ImageField(upload_to=get_img_path, null=True, blank=True,
                                verbose_name='缩略图')
    #group = models.ManyToManyField(NodeGroup, related_name='group_to_node', null=True, blank=True, verbose_name='栏目组')

    node_path = models.CharField(max_length=200, default='0', verbose_name='栏目路径')
    node_level = models.IntegerField(default=1, verbose_name='栏目级别', help_text='1为一级,2为二级,3为三级,以此类推')

    seo_title = models.CharField(max_length=200, blank=True, null=True, verbose_name='seo title')
    seo_keywords = models.CharField(max_length=200, blank=True, null=True, verbose_name='seo keywords')
    seo_description = models.CharField(max_length=200, blank=True, null=True, verbose_name='seo description')

    class Meta:
        verbose_name = '栏目管理'
        verbose_name_plural = verbose_name
        ordering = ['order_num', 'id']

    def __str__(self):
        return  '|' + '--'* (self.node_level-1) + self.name + ',' + (self.node_type if self.node_type != "" and self.node_type is not None else "")


class ArticleGroup(models.Model):
    name = models.CharField(max_length=200, verbose_name='文章组名称')
    index_name = models.CharField(max_length=100, null=True, blank=True, unique=True, verbose_name='组索引')
    picture = models.ImageField(upload_to=get_img_path, null=True, blank=True, verbose_name='文章组图片')
    site = models.ForeignKey(Sites, on_delete=models.CASCADE, verbose_name='所属站点')
    brief = models.TextField(null=True, blank=True, verbose_name='简介')
    content = models.TextField(null=True, blank=True, verbose_name='详情')
    article = models.ManyToManyField('Article', related_name='article_group', null=True, blank=True, verbose_name='组内文章')

    class Meta:
        verbose_name = '文章组管理'
        verbose_name_plural = verbose_name

    def __str__(self):
        return self.name


class Article(models.Model):
    parent_node = models.ForeignKey(Node, on_delete=models.DO_NOTHING, related_name='node_to_article', verbose_name='所属栏目')
    title = models.CharField(max_length=200, verbose_name='标题')
    sub_title = models.CharField(max_length=200, null=True, blank=True, verbose_name='副标题')
    #suggest = models.ManyToManyField(SuggestManage, blank=True, null=True, related_name='article', verbose_name='推荐到')
    order_num = models.IntegerField(default=0, verbose_name='排序')
    hit = models.IntegerField(default=0, verbose_name='点击次数')
    force_url = models.CharField(max_length=255, null=True, blank=True, verbose_name='强制链接')
    picture = models.ImageField(upload_to=get_img_path, null=True, blank=True, verbose_name='图片')
    video = models.FileField(upload_to=get_video_path, null=True, blank=True, verbose_name='视频')
    file = models.FileField(upload_to=get_file_path, null=True, blank=True, verbose_name='相关附件')
    add_time = models.DateTimeField(default=timezone.now, verbose_name='添加日期')
    is_active = models.BooleanField(default=True, verbose_name='是否激活')
    is_delete = models.BooleanField(default=False, verbose_name='是否已删除')
    is_search = models.BooleanField(default=True, verbose_name='是否能被搜索')
    site = models.ForeignKey(Sites, on_delete=models.CASCADE, verbose_name='所属站点')
    seo_title = models.TextField(null=True, blank=True, verbose_name='SEO标题')
    seo_keyword = models.TextField(null=True, blank=True, verbose_name='SEO关键字')
    seo_description = models.TextField(null=True, blank=True, verbose_name='SEO描述')

    # 扩展字段
    picture2 = models.ImageField(upload_to=get_img_path, null=True, blank=True, verbose_name='图片2')
    picture3 = models.ImageField(upload_to=get_img_path, null=True, blank=True, verbose_name='图片3')
    picture4 = models.ImageField(upload_to=get_img_path, null=True, blank=True, verbose_name='图片4')
    video2 = models.FileField(upload_to=get_video_path, null=True, blank=True, verbose_name='视频2')
    video3 = models.FileField(upload_to=get_video_path, null=True, blank=True, verbose_name='视频3')
    video4 = models.FileField(upload_to=get_video_path, null=True, blank=True, verbose_name='视频4')
    file2 = models.FileField(upload_to=get_file_path, null=True, blank=True, verbose_name='附件2')
    file3 = models.FileField(upload_to=get_file_path, null=True, blank=True, verbose_name='附件3')
    file4 = models.FileField(upload_to=get_file_path, null=True, blank=True, verbose_name='附件4')

    input1 = models.CharField(max_length=200, null=True, blank=True, verbose_name='单行文本框1')
    input2 = models.CharField(max_length=200, null=True, blank=True, verbose_name='单行文本框2')
    input3 = models.CharField(max_length=200, null=True, blank=True, verbose_name='单行文本框3')
    input4 = models.CharField(max_length=200, null=True, blank=True, verbose_name='单行文本框4')
    input5 = models.CharField(max_length=200, null=True, blank=True, verbose_name='单行文本框5')

    textarea1 = models.TextField(null=True, blank=True, verbose_name='多行文本框1')
    textarea2 = models.TextField(null=True, blank=True, verbose_name='多行文本框2')
    textarea3 = models.TextField(null=True, blank=True, verbose_name='多行文本框3')
    textarea4 = models.TextField(null=True, blank=True, verbose_name='多行文本框4')
    textarea5 = models.TextField(null=True, blank=True, verbose_name='多行文本框5')

    brief = models.TextField(null=True, blank=True,  verbose_name='简介')
    content = models.TextField(null=True, blank=True, verbose_name='详情')

    class Meta:
        verbose_name = '文章管理'
        verbose_name_plural = verbose_name
        ordering = ('-add_time', '-order_num')

    def __str__(self):
        return self.title

    @property
    def get_href(self):
        '获取文章链接'
        from xadmin.plugins import GetTemplate as tmp
        temp = tmp.get_content_template(self.parent_node_id)
        href = reverse(temp.router, args=(self.id,))
        print('------href:', href)
        return href



class AuditLog(models.Model):
    user = models.ForeignKey(UserProfile, on_delete=models.DO_NOTHING, verbose_name='用户')
    add_time = models.DateTimeField(auto_now_add=True, verbose_name='时间')
    action = models.CharField(max_length=500, verbose_name='操作')
    site = models.ForeignKey(Sites, null=True, on_delete=models.DO_NOTHING, verbose_name='所属站点')

    class Meta:
        verbose_name = '审记日志'
        verbose_name_plural = verbose_name
        ordering = ('-add_time', '-id')

    def __str__(self):
        return self.user.action


class Part(models.Model):
    name = models.CharField(max_length=200, verbose_name='名称')
    site = models.ForeignKey(Sites, on_delete=models.CASCADE, verbose_name='所属站点')
    index_name = models.CharField(max_length=50, unique=True, verbose_name='标识')

    class Meta:
        verbose_name = '部件'
        verbose_name_plural = verbose_name

    def __str__(self):
        return self.name

etype_choices = (
    ('input', '单行文本框'),
    ('text', '多行文本框'),
    ('editor', '富文本框'),
    ('image', '图片'),
    ('file', '文件'),
    ('video', '视频')
)

class PartElement(models.Model):
    name = models.CharField(max_length=200, verbose_name='标题')
    slogen1 = models.CharField(max_length=200, null=True, blank=True, verbose_name='slogen1')
    slogen2 = models.CharField(max_length=200, null=True, blank=True, verbose_name='slogen2')
    etype = models.CharField(max_length=50, choices=etype_choices, verbose_name='类型')
    part = models.ForeignKey(Part, on_delete=models.CASCADE, verbose_name='所属部件')
    input = models.CharField(max_length=200, null=True, blank=True)
    text = models.TextField(null=True, blank=True)
    image = models.ImageField(upload_to=get_img_path, null=True, blank=True)
    file = models.FileField(upload_to=get_file_path, null=True, blank=True)
    video = models.FileField(upload_to=get_video_path, null=True, blank=True)

    class Meta:
        verbose_name = '部件元素'
        verbose_name_plural = verbose_name

    def __str__(self):
        return self.name
